﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class DonViViewModel : INotifyPropertyChanged
    {
        public DonViViewModel()
        {
            RefreshDonVi();
            RefreshKhoa();
            this.PropertyChanged += (s, e) => {
                if (insertCurDonVi != null)
                    insertCurDonVi.RaiseCanExecuteChanged();
                if (saveCurDonVi != null)
                    saveCurDonVi.RaiseCanExecuteChanged();
                if (delCurDonVi != null)
                    delCurDonVi.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<DONVI> donVis;
        public IEnumerable<DONVI> DonVis
        {
            get { return this.donVis; }
            set
            {
                this.donVis = value;
                OnPropertyChange("DonVis");
            }
        }


        private DONVI currentDonVi = new DONVI();

        public DONVI CurrentDonVi
        {
            get { return this.currentDonVi; }
            set
            {
                this.currentDonVi = value;
                OnPropertyChange("CurrentDonVi");
                if (this.CurrentDonVi != null && this.CurrentDonVi.ID != 0)
                {
                    RefreshCurKhoa();
                }
            }
        }
        private IEnumerable<KHOA> khoas;
        public IEnumerable<KHOA> Khoas
        {
            get { return this.khoas; }
            set
            {
                this.khoas = value;
                OnPropertyChange("Khoas");
                if (this.Khoas!=null && this.CurrentKhoa != null && this.CurrentKhoa.ID==0)
                    this.CurrentKhoa = Khoas.First();
            }
        }
        
        private KHOA currentKhoa= new KHOA();
        public KHOA CurrentKhoa 
        {
            get { return this.currentKhoa; }
            set
            {
                this.currentKhoa = value;
                OnPropertyChange("CurrentKhoa");
                
            }
        }
       
        #endregion

        #region refreshPhong
        private void RefreshDonVi()
        {

            this.ServiceClient.getAllDonViCompleted += (s, e) =>
            {
                this.DonVis = e.Result;
            };
            this.ServiceClient.getAllDonViAsync();
        }

        private void RefreshKhoa()
        {
            //throw new NotImplementedException();
            
            this.ServiceClient.getAllKhoaCompleted += (s, e) =>
            {
                this.Khoas = e.Result;
                
            };
            this.ServiceClient.getAllKhoaAsync();
            

        }
        private void RefreshCurKhoa()
        {
             CurrentKhoa=Khoas.Where(x => x.ID == CurrentDonVi.KhoaID).First();
        }
        #endregion
        
        #region Save
        private RelayCommand saveCurDonVi;
        public ICommand SaveCurDonVi
        {
            get { return saveCurDonVi ?? (saveCurDonVi = new RelayCommand(() => SaveDonVi(), () => this.CurrentDonVi != null && this.CurrentDonVi.ID != 0)); }
        }

        private void SaveDonVi()
        {
            CurrentDonVi.KhoaID = CurrentKhoa.ID;
            ServiceClient.saveDonViAsync(CurrentDonVi);
            RefreshDonVi();
        }

        
        #endregion

        #region del
        private RelayCommand delCurDonVi;
        public ICommand DelCurDonVi
        {
            get { return delCurDonVi ?? (delCurDonVi = new RelayCommand(() => DelDonVi(), () => this.CurrentDonVi != null && this.CurrentDonVi.ID != 0)); }
        }

        private void DelDonVi()
        {
            ServiceClient.deleteDonViAsync(CurrentDonVi);
            RefreshDonVi();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurDonVi;
        public ICommand InsertCurDonVi
        {
            get { return insertCurDonVi ?? (insertCurDonVi = new RelayCommand(() => InsertDonVi(), () => this.CurrentDonVi != null)); }
        }

        private void InsertDonVi()
        {
            CurrentDonVi.KhoaID = CurrentKhoa.ID;
            ServiceClient.insertDonViAsync(CurrentDonVi);
            RefreshDonVi();
        }
        #endregion
        
    }
}
